const { ipcRenderer } = require('electron')
const getIPCConst = import('../src/consts/ipc.js')

const windowLoaded = new Promise((resolve) => {
  window.onload = resolve
})

ipcRenderer.on('main-world-port', async (event) => {
  await windowLoaded
  const { MAIN_WORLD_PORT } = await getIPCConst
  window.postMessage(MAIN_WORLD_PORT, '*', event.ports)
})

ipcRenderer.on('child-to-child-world-port', async (event, data) => {
  await windowLoaded
  const { CHILD_TO_CHILD_WORLD_PORT } = await getIPCConst
  window.postMessage(
    {
      data,
      type: CHILD_TO_CHILD_WORLD_PORT,
    },
    '*',
    event.ports,
  )
})
